﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.BUS;
using ECare.BUS.Data;

namespace ECare.Client.frms
{
    public partial class frmRole : frmGridTemplate
    {
        public frmRole()
        {
            InitializeComponent();
            this.Load += frmRole_Load;
        }

        public BindingList<RoleInfo> source { get; set; }
        void frmRole_Load(object sender, EventArgs e)
        {
            source = new BindingList<RoleInfo>(RoleInfoHelper.GetRoles());
            this.eGridView1.DataSource = source;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var frm = new frmRoleInfo();
            frm.entity = null;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<RoleInfo>();
            var role = entity as RoleInfo;
            var findresult = source.FirstOrDefault(a=>a.RoleId==role.RoleId);
            if (findresult == null)
                this.source.Add(role);
        }

        private void eGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var entitye = this.eGridView1.Rows[e.RowIndex].DataBoundItem as RoleInfo;
            var frm = new frmRoleInfo();
            frm.entity = entitye;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.eGridView1.SelectedRows.Count == 0)
                return;

            var entitye = this.eGridView1.SelectedRows[0].DataBoundItem as RoleInfo;
            var frm = new frmRoleInfo();
            frm.entity = entitye;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }
    }
}
